<?php
session_start();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>考勤管理系统 - 安装向导</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .install-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .install-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        .install-body {
            padding: 2rem;
        }
        .step-indicator {
            display: flex;
            justify-content: space-between;
            margin-bottom: 2rem;
            position: relative;
        }
        .step-indicator::before {
            content: '';
            position: absolute;
            top: 20px;
            left: 0;
            right: 0;
            height: 2px;
            background: #e9ecef;
            z-index: 1;
        }
        .step {
            text-align: center;
            position: relative;
            z-index: 2;
        }
        .step-number {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #e9ecef;
            color: #6c757d;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 0.5rem;
            font-weight: bold;
        }
        .step.active .step-number {
            background: #667eea;
            color: white;
        }
        .step.completed .step-number {
            background: #28a745;
            color: white;
        }
        .step-label {
            font-size: 0.875rem;
            color: #6c757d;
        }
        .step.active .step-label {
            color: #667eea;
            font-weight: bold;
        }
        .requirement-check {
            margin-bottom: 1rem;
        }
        .requirement-check i {
            width: 20px;
            text-align: center;
        }
        .requirement-check.success {
            color: #28a745;
        }
        .requirement-check.error {
            color: #dc3545;
        }
        .requirement-check.warning {
            color: #ffc107;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="install-container">
                    <div class="install-header">
                        <h1><i class="fas fa-clock me-2"></i>考勤管理系统</h1>
                        <p class="mb-0">安装向导</p>
                    </div>
                    
                    <div class="install-body">
                        <!-- 步骤指示器 -->
                        <div class="step-indicator">
                            <div class="step active">
                                <div class="step-number">1</div>
                                <div class="step-label">环境检测</div>
                            </div>
                            <div class="step">
                                <div class="step-number">2</div>
                                <div class="step-label">数据库配置</div>
                            </div>
                            <div class="step">
                                <div class="step-number">3</div>
                                <div class="step-label">系统安装</div>
                            </div>
                            <div class="step">
                                <div class="step-number">4</div>
                                <div class="step-label">完成安装</div>
                            </div>
                        </div>

                        <h3 class="mb-4">环境检测</h3>
                        
                        <?php
                        // 环境检测
                        $requirements = [];
                        
                        // PHP版本检测
                        $phpVersion = phpversion();
                        $phpRequired = '7.4.0';
                        $phpCheck = version_compare($phpVersion, $phpRequired, '>=');
                        $requirements[] = [
                            'name' => 'PHP版本',
                            'required' => $phpRequired,
                            'current' => $phpVersion,
                            'status' => $phpCheck ? 'success' : 'error',
                            'message' => $phpCheck ? '符合要求' : 'PHP版本需要 ' . $phpRequired . ' 或更高版本'
                        ];
                        
                        // MySQL扩展检测
                        $mysqlCheck = extension_loaded('pdo_mysql');
                        $requirements[] = [
                            'name' => 'MySQL扩展',
                            'required' => '需要',
                            'current' => $mysqlCheck ? '已安装' : '未安装',
                            'status' => $mysqlCheck ? 'success' : 'error',
                            'message' => $mysqlCheck ? 'MySQL扩展已安装' : '需要安装PDO MySQL扩展'
                        ];
                        
                        // 文件权限检测
                        $writableDirs = ['../includes', '../assets', '../install'];
                        foreach ($writableDirs as $dir) {
                            // 如果目录不存在，尝试创建它
                            if (!file_exists($dir)) {
                                if (mkdir($dir, 0755, true)) {
                                    $isWritable = true;
                                    $message = '目录已自动创建';
                                } else {
                                    $isWritable = false;
                                    $message = '目录不存在且无法自动创建';
                                }
                            } else {
                                $isWritable = is_writable($dir);
                                $message = $isWritable ? '目录可写' : '目录需要写权限';
                            }
                            
                            $requirements[] = [
                                'name' => '目录权限: ' . $dir,
                                'required' => '可写',
                                'current' => $isWritable ? '可写' : '不可写',
                                'status' => $isWritable ? 'success' : 'error',
                                'message' => $message
                            ];
                        }
                        
                        // Session支持
                        $sessionCheck = function_exists('session_start');
                        $requirements[] = [
                            'name' => 'Session支持',
                            'required' => '需要',
                            'current' => $sessionCheck ? '支持' : '不支持',
                            'status' => $sessionCheck ? 'success' : 'error',
                            'message' => $sessionCheck ? 'Session支持正常' : '需要Session支持'
                        ];
                        
                        // JSON支持
                        $jsonCheck = function_exists('json_encode');
                        $requirements[] = [
                            'name' => 'JSON支持',
                            'required' => '需要',
                            'current' => $jsonCheck ? '支持' : '不支持',
                            'status' => $jsonCheck ? 'success' : 'error',
                            'message' => $jsonCheck ? 'JSON支持正常' : '需要JSON支持'
                        ];
                        
                        // 检查所有要求是否满足
                        $allRequirementsMet = true;
                        foreach ($requirements as $req) {
                            if ($req['status'] === 'error') {
                                $allRequirementsMet = false;
                                break;
                            }
                        }
                        ?>
                        
                        <div class="mb-4">
                            <?php foreach ($requirements as $req): ?>
                            <div class="requirement-check <?php echo $req['status']; ?>">
                                <i class="fas fa-<?php 
                                    echo $req['status'] === 'success' ? 'check' : 
                                         ($req['status'] === 'warning' ? 'exclamation-triangle' : 'times'); 
                                ?> me-2"></i>
                                <strong><?php echo $req['name']; ?>:</strong>
                                <?php echo $req['current']; ?> 
                                (要求: <?php echo $req['required']; ?>)
                                <small class="text-muted d-block"><?php echo $req['message']; ?></small>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        
                        <?php if ($allRequirementsMet): ?>
                            <div class="alert alert-success">
                                <i class="fas fa-check-circle me-2"></i>
                                所有环境要求都已满足，可以继续安装。
                            </div>
                            <div class="text-end">
                                <a href="database.php" class="btn btn-primary">
                                    下一步 <i class="fas fa-arrow-right ms-2"></i>
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-danger">
                                <i class="fas fa-exclamation-triangle me-2"></i>
                                部分环境要求未满足，请解决上述问题后再继续安装。
                            </div>
                            <div class="text-end">
                                <button class="btn btn-secondary" disabled>
                                    下一步 <i class="fas fa-arrow-right ms-2"></i>
                                </button>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
